package com.Merlin.myapplication

import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.Merlin.myapplication.model.CityData


class CityAdapter(val cityList: List<CityData>) : BaseAdapter() {
    override fun getCount(): Int {
        return cityList.size
    }

    override fun getItem(p0: Int): Any {
        return cityList[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        // 1. 获取单个城市数据
        val cityData = cityList[p0]
        // 2. 从XML文件中获取布局
        // 创建并返回视图，设置其中的图标和文本
        // View.inflate() 是用于将 XML 布局文件转换为实际 View 对象的静态方法
        return View.inflate(p2?.context, R.layout.item_city_plus, null).apply {
            findViewById<ImageView>(R.id.cityIcon).setImageResource(cityData.icon)
            findViewById<TextView>(R.id.cityText).text = cityData.text
            findViewById<TextView>(R.id.cityIntro).text = cityData.intro
        }
    }
}